/**
 * 分页js文件
 */
$(function(){
	var url = $("#url").val()					/* 页码url */
	var pageCount = $("#pageCount").val()		/* 总页数 */
	var currentPage = $("#currentPage").val()	/* 当前页数 */
	var showPageCount = 5 						/* 设置最多显示的页数 */
	var min = 1									/* 分页最小页数 */
	var max = 1									/* 分页最大页数 */
	
	pageMunber()
	drawPage()
	
	/* 动态加载当前页数为显示的中间页数 */
	function pageMunber(){
		if(pageCount <= showPageCount){
			min = 1;
			max = pageCount;
		}else if(parseInt(currentPage)+Math.floor(showPageCount/2)>pageCount){
			min = pageCount - showPageCount + 1
			max = pageCount
		}else if(currentPage-Math.floor(showPageCount/2)<=0){
			min = 1
			max = showPageCount
		}else{
			min = parseInt(currentPage)-Math.floor(showPageCount/2)
			max = parseInt(currentPage)+Math.floor(showPageCount/2)
		}
	}
	
	/* 画分页 */
	function drawPage(){
		var html = "<ul><li><a href='#' id='befor'>上一页</a></li>"
		for(var pageNumber=min;pageNumber<=max;pageNumber++){
			if(pageNumber == currentPage){
				html += "<li style='color:red;'>"+pageNumber+"</li>"
			}else{
				html += "<li><a href='"+url+pageNumber+"'>"+pageNumber+"</a></li>"
			}
		}
		html += "<li><a href='#' id='next'>下一页</a></li></ul>"
		$("#showPageContent").html(html)
	}

	$("#showPageContent").on("click","#befor",function(){
		if(currentPage != 1){
			$("#befor").attr("url",url+Math.floor(currentPage-1))
			newUrl = url+Math.floor(currentPage-1)
			window.location.href = newUrl
		}else{
			return false
			alert("没有页了")
		}
	})
	
	$("#showPageContent").on("click","#next",function(){
		if(currentPage != pageCount){
			$("#next").attr("url",url+Math.floor((parseInt(currentPage)+1)))
			newUrl = url+Math.floor((parseInt(currentPage)+1))
			window.location.href = newUrl
		}else{
			return false
			alert("没有页了")
		}
	})
})